class_name SceneTorch
extends Node2D

@onready var particle: GPUParticles2D = $Fire/Particle
@onready var point_light_2d: PointLight2D = $Fire/PointLight2D

@export var fire_max_energy: float = 1.5
@export var fire_open_time: float = 1.0

@export var is_open: bool = false
var data: TorchData


func init_by_data(d: TorchData):
	self.data = d
	if data.is_on:
		open()
	else:
		close()


func _ready() -> void:
	if not data:
		data = TorchData.new()
		data.id = self.name
		data.is_on = false
	
	if is_open:
		open()


func _on_hit_area_area_entered(area: Area2D) -> void:
	open()


func open():
	GlobalUtils.start_particle_emit(particle)
	GlobalUtils.tween_light_on(point_light_2d, fire_max_energy, fire_open_time)
	
	data.is_on = true
	
	
func close():
	GlobalUtils.stop_particle_emit(particle)
	GlobalUtils.tween_light_off(particle, point_light_2d, fire_open_time)
	
	data.is_on = false
